// @flow
import { type Map } from 'immutable';

export const socialConnectConfigure = {
  weibo: {
    id: '2033652754',
    name: 'weibo',
    oauth: {
      version: 2,
      auth: 'https://api.weibo.com/oauth2/authorize',
      grant: 'https://api.weibo.com/oauth2/access_token',
      scope: { email: 'email' },
      response_type: 'code',
      response_kind: 'post',
      grant_type: 'authorization_code',
      grant_kind: 'post',
    },
    scope: { email: 'email' },
    base: 'https://api.weibo.com/2/',
  },
  github: {
    id: '3eeab94042f0cb567195',
    name: 'github',
    oauth: {
      version: 2,
      auth: 'https://github.com/login/oauth/authorize',
      grant: 'https://github.com/login/oauth/access_token',
      response_type: 'code',
      response_kind: 'post',
      grant_type: 'authorization_code',
      grant_kind: 'post',
    },
    scope: { email: 'user:email' },
    base: 'https://api.github.com/',
  },
  google: {
    id: '872992505441-ukcv5bqt5pigi1jna6q699gm1uce3qh0.apps.googleusercontent.com',
    name: 'Google Plus',
    // See: http://code.google.com/apis/accounts/docs/OAuth2UserAgent.html
    oauth: {
      version: 2,
      auth: 'https://accounts.google.com/o/oauth2/auth',
      grant: 'https://accounts.google.com/o/oauth2/token',
      response_type: 'code',
      response_kind: 'get',
      grant_type: 'authorization_code',
      grant_kind: 'get',
    },
    // Authorization scopes
    scope: {
      basic: 'https://www.googleapis.com/auth/plus.me profile',
      email: 'email',
      birthday: '',
      events: '',
      photos: 'https://picasaweb.google.com/data/',
      videos: 'http://gdata.youtube.com',
      friends: 'https://www.google.com/m8/feeds, https://www.googleapis.com/auth/plus.login',
      files: 'https://www.googleapis.com/auth/drive.readonly',
      publish: '',
      publish_files: 'https://www.googleapis.com/auth/drive',
      share: '',
      create_event: '',
      offline_access: '',
    },
    // API base URI
    base: 'https://www.googleapis.com/',
  },
};
export const redirectUri =
  process.env.NODE_ENV === 'development'
    ? 'http://127.0.0.1:3000/response'
    : 'http://melog.co/response';
const popFeatures = {
  toolbar: true,
  location: true,
  directories: false,
  status: false,
  menubar: true,
  scrollbars: 'yes',
  resizable: 'no',
  copyhistory: 'yes',
  width: '600px',
  height: '400px',
};
/**
 * Convert features props to window features format (name=value,other=value).
 * @param {Object} obj
 * @return {String}
 * @public
 */
export function toWindowFeatures(obj: any) {
  const featuresArr = [];
  Object.keys(obj).forEach((item) => {
    const value = obj[item];
    if (typeof value === 'boolean') {
      featuresArr.push(`${item}=${value ? 'yes' : 'no'}`);
    } else {
      featuresArr.push(`${item}=${value}`);
    }
  });
  return featuresArr.join(',');
}
export const login = (
  windowName: string,
  provider: string,
  scope: string,
  redirectUrl: string,
  features: any = popFeatures,
) => {
  const inst = socialConnectConfigure[provider];
  // console.log(inst);
  const scopestr = scope.split(',').map((item, key) => {
    return `${scopestr}&scope=${inst.scope[item]}`;
    // console.log(scopestr);
  });
  const url = `${inst.oauth.auth}?client_id=${inst.id}&response_type=${
    inst.oauth.response_type
  }${scopestr}&redirect_uri=${redirectUrl}`;
  // console.log(url);
  const featuresStr = toWindowFeatures(features);
  return { url, featuresStr };
};
